<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once('Entity/Doc_Member_Download_Entity.php');        
class Doc_Member_Download_Model extends Doc_Member_Download_Entity
{  
    public function __construct()
    {
        parent::__construct();
    }
    
    public function addDownload($docId, $memberId, $id = 0, $numdow = 0)
    {
        if ($id != 0) {
            $this->setId($id);
            $this->setDown($numdow);
        } else {
            $this->setDown(1);
        }
        $this->setDid($docId);
        $this->setMid($memberId);
        $this->setDday(time());
        $this->save();
    }
    
    public function checkDown($docId, $memberId)
    {
        $sql = "SELECT id, down FROM doc_member_download WHERE did = ? AND mid = ? ";
        $query = $this->db->query($sql, array($docId, $memberId));
        if ($query->num_rows() > 0) {
            return $query->row();
        }
        return 0;
    }
    
    public function checkdownOnday($memberId)
    {
        $sql = "SELECT count(*) as total FROM doc_member_download WHERE mid = ? AND FROM_UNIXTIME(dday, '%Y-%m-%d') = CURRENT_DATE() ";
        $query = $this->db->query($sql, array($memberId));
        $obj = $query->row();
        if ($obj) {
            return $obj->total;
        } else {
            return 0;
        }
    }
    
    public function numDownload_byDocId($docId)
    {
        $sql = "SELECT count(*) as total FROM doc_member_download WHERE did = ? ";
        $query = $this->db->query($sql, array($docId));
        $obj = $query->row();
        if ($obj) {
            return $obj->total;
        } else {
            return 0;
        }
    }
}
